package net.chasing.androidbaseconfig.util.glide;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.res.ResourcesCompat;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.FitCenter;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;

import net.chasing.androidbaseconfig.R;
import net.chasing.androidbaseconfig.util.FileUtils;
import net.chasing.androidbaseconfig.util.MediaUtils;
import net.chasing.androidbaseconfig.util.glide.base.GlideRoundTransform;

import java.io.File;

/**
 * 整体进行tray/catch，图片加载出错不影响使用
 * 注:
 * 1、Glide进行加载会清除ImageView的src，所以占位图只能通过placeholder进行设置
 * 圆形占位图实现方式：   .placeholder(new CircleRoundDrawable(context, placeholder))
 * 2、Glide4不允许在onFail里面继续调用Glide.load()进行加载
 * 可以在onFail里面通过Handler进行Glide.load()
 * 3、error要设置centerCrop模式的话，可以直接通过.error(Glide.with().load().centerCrop())
 */
public class ImageLoadUtils {
    private ImageLoadUtils() {
    }

    public static void resume(Context context) {
        Glide.with(context).resumeRequests();
    }

    public static void pause(Context context) {
        Glide.with(context).pauseRequests();
    }

    // 取消加载
    public static void cancel(Context context) {
        try {
            Glide.with(context).onDestroy();
        } catch (Exception ignored) {
        }
    }

    // 检测参数是否不合法，不合法则不进行加载图片
    public static boolean checkParamsIllegal(Context context, ImageView imageView) {
        return context == null || imageView == null;
    }

    // 加载GIF，并且只播放一次
    public static void loadGifResourceOnce(Context context, ImageView imageView, int resId) {
        AnimationDrawable anim = (AnimationDrawable) ResourcesCompat.getDrawable(context.getResources(), resId, null);
        if (anim != null) {
            anim.setOneShot(true);
            imageView.setImageDrawable(anim);
            anim.start();
        }
    }

    private static RequestBuilder<Drawable> getDrawableRequest(Context context, String url) {
        RequestBuilder<Drawable> requestBuilder;
        if (FileUtils.isFilePath(url)) {
            // 私有文件可以直接加载，公共文件才需要转换成uri进行加载
            if (FileUtils.isSelfFile(context, url))
                requestBuilder = Glide.with(context).load(new File(url));
            else {
                Uri fileUri = FileUtils.getFileUri(context, url);
                requestBuilder = Glide.with(context).load(fileUri);
            }
        } else {
            requestBuilder = Glide.with(context).load(url);
        }
        return requestBuilder;
    }

    private static RequestBuilder<Bitmap> getBitmapRequest(Context context, String url) {
        RequestBuilder<Bitmap> requestBuilder;
        if (FileUtils.isFilePath(url)) {
            // 私有文件可以直接加载，公共文件才需要转换成uri进行加载
            if (FileUtils.isSelfFile(context, url))
                requestBuilder = Glide.with(context).asBitmap().load(new File(url));
            else {
                Uri fileUri = FileUtils.getFileUri(context, url);
                requestBuilder = Glide.with(context).asBitmap().load(fileUri);
            }
        } else {
            requestBuilder = Glide.with(context).asBitmap().load(url);
        }
        return requestBuilder;
    }

    // region 加载用户头像
    public static void loadHeadImg(Context context, ImageView imageView, String headImg) {
        if (checkParamsIllegal(context, imageView)) return;
        try {
            getDrawableRequest(context, headImg)
                    .placeholder(R.mipmap.head_default)
                    .transform(new CenterCrop(), new GlideRoundTransform(90))
                    .error(createRoundErrorRequestBuilder(context, TYPE_HEAD, 90))
                    .into(imageView);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadHeadImg(Context context, ImageView imageView, int resId) {
        if (checkParamsIllegal(context, imageView)) return;
        try {
            Glide.with(context)
                    .load(resId)
                    .placeholder(R.mipmap.head_default)
                    .transform(new CenterCrop(), new GlideRoundTransform(90))
                    .into(imageView);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadHeadImgWithStroke(Context context, ImageView imageView, String headImg,
                                             int strokeWidth, int strokeColor) {
        if (checkParamsIllegal(context, imageView)) return;
        try {
            getDrawableRequest(context, headImg)
                    .placeholder(R.mipmap.head_default)
                    .transform(new CenterCrop(), new GlideRoundTransform(90, strokeWidth, strokeColor))
                    .error(createRoundErrorRequestBuilderWithStroke(context, TYPE_HEAD, 90, strokeWidth, strokeColor))
                    .into(imageView);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadHeadImgWithStroke(Context context, ImageView imageView, String headImg,
                                             int strokeWidth, int strokeColor, int backgroundColor) {
        if (checkParamsIllegal(context, imageView)) return;
        try {
            getDrawableRequest(context, headImg)
                    .placeholder(R.mipmap.head_default)
                    .transform(new CenterCrop(), new GlideRoundTransform(90, strokeWidth, strokeColor).setBackgroundColor(backgroundColor))
                    .error(createRoundErrorRequestBuilderWithStroke(context, TYPE_HEAD, 90, strokeWidth, strokeColor))
                    .into(imageView);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // endregion

    // region 无圆角，需要在ImageView上设置CenterCrop，Glide不会为error图设置加载类型
    // region 不区分图片、gif，gif会进行播放
    public static void load(Context context, ImageView iv, int resId) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            Glide.with(context)
                    .load(resId)
                    .centerCrop()
                    .into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void load(Context context, ImageView iv, int resId, int width, int height) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            Glide.with(context)
                    .load(resId)
                    .centerCrop()
                    .override(width, height)
                    .into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void load(final Context context, final ImageView iv, String url) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            getDrawableRequest(context, url)
                    .centerCrop()
                    .into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void load(final Context context, final ImageView iv, String url, byte errorType) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            getDrawableRequest(context, url)
                    .centerCrop()
                    .error(createErrorRequestBuilder(context, getErrorByType(errorType)))
                    .into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void load(final Context context, ImageView iv, String url, int errorResId) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            //设置CenterCrop是为了适配placeHolder，在onResourceReady重新切换会FitCenter，ImageView默认是FitCenter
            getDrawableRequest(context, url)
                    .centerCrop()
                    .error(createErrorRequestBuilder(context, errorResId))
                    .into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadFitCenter(final Context context, final ImageView iv, String url, final int errorResId, int placeholder) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            //设置CenterCrop是为了适配placeHolder，在onResourceReady重新切换会FitCenter，ImageView默认是FitCenter
            iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
            getDrawableRequest(context, url)
                    .centerCrop()
                    .placeholder(placeholder)
                    .dontAnimate()
                    .error(createErrorRequestBuilder(context, errorResId))
                    .listener(new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                            iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                            iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
                            return false;
                        }
                    })
                    .into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void load(Context context, ImageView iv, String url, int errorResId, float thumbnail, int width, int height) {
        if (checkParamsIllegal(context, iv)) return;

        try {
            RequestBuilder<Drawable> requestBuilder = getDrawableRequest(context, url).override(width, height)
                    .centerCrop();
            if (errorResId > 0)
                requestBuilder = requestBuilder.error(createErrorRequestBuilder(context, errorResId));
            requestBuilder.thumbnail(thumbnail)
                    .into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // endregion

    // region 进加载成图片，asBitmap
    public static void loadBitmap(Context context, ImageView iv, int resId) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            Glide.with(context)
                    .asBitmap()
                    .load(resId)
                    //.diskCacheStrategy(DiskCacheStrategy.NONE)
                    .centerCrop()
                    .into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadBitmap(Context context, ImageView iv, String url, int errorResId) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            getBitmapRequest(context, url)
                    .centerCrop()
                    .error(createErrorRequestBuilderAsBitmap(context, errorResId))
                    .into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadBitmap(Context context, ImageView iv, String url, int errorResId, int placeHolder) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
            RequestBuilder<Bitmap> requestBuilder = getBitmapRequest(context, url)
                    .centerCrop()
                    .placeholder(placeHolder)
                    .dontAnimate()
                    .listener(new RequestListener<Bitmap>() {
                        @Override
                        public boolean onLoadFailed(GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                            iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                            iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
                            return false;
                        }
                    });
            if (errorResId > 0)
                requestBuilder = requestBuilder.error(createErrorRequestBuilderAsBitmap(context, errorResId));
            requestBuilder.into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadBitmap(Context context, ImageView iv, String url, int errorResId, int width, int height) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            RequestBuilder<Bitmap> requestBuilder = getBitmapRequest(context, url)
                    .centerCrop()
                    .error(createErrorRequestBuilderAsBitmap(context, errorResId));
            if (width > 0 && height > 0)
                requestBuilder = requestBuilder.override(width, height);
            requestBuilder.thumbnail(0.5f);
            requestBuilder.into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // endregion

    // region 不进行默认centerCrop处理的

    public static void loadWithoutScale(Context context, ImageView iv, int resId) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            Glide.with(context)
                    .load(resId)
                    .into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadWithoutScale(Context context, ImageView iv, String url, int errorResId) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            getDrawableRequest(context, url)
                    .error(errorResId)
                    .into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // endregion

    private static RequestBuilder createErrorRequestBuilder(Context context, int errorResId) {
        return Glide.with(context).load(errorResId).centerCrop();
    }

    private static RequestBuilder createErrorRequestBuilderAsBitmap(Context context, int errorResId) {
        return Glide.with(context).asBitmap().load(errorResId).centerCrop();
    }
    // endregion

    // region 有圆角，不能在ImageView上设置CenterCrop，Glide的圆角裁剪会和ImageView自身的CenterCrop冲突，CenterCrop由Glide设置
    // region asBitmap形式
    public static void loadRoundBitmap(Context context, ImageView iv, int resId, int corner) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            Glide.with(context)
                    .asBitmap()
                    .load(resId)
                    .transform(new CenterCrop(), new GlideRoundTransform(corner))
                    .into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadRoundBitmap(Context context, ImageView iv, int resId, int corner, int width, int height) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            RequestBuilder<Bitmap> transform = Glide.with(context)
                    .asBitmap()
                    .load(resId)
                    .transform(new CenterCrop(), new GlideRoundTransform(corner));
            if (width > 0 && height > 0)
                transform = transform.override(width, height);
            transform.into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadRoundBitmap(Context context, ImageView iv, String url, byte errorType, int corner) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            getBitmapRequest(context, url)
                    .transform(new CenterCrop(), new GlideRoundTransform(corner))
                    .error(createRoundErrorRequestBuilderAsBitmap(context, errorType, corner))
                    .into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadRoundBitmap(Context context, ImageView iv, String url, byte errorType,
                                       int corner, int backgroundColor) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            getBitmapRequest(context, url)
                    .transform(new CenterCrop(), new GlideRoundTransform(corner).setBackgroundColor(backgroundColor))
                    .error(createRoundErrorRequestBuilderAsBitmap(context, errorType, corner))
                    .into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadRoundBitmap(Context context, ImageView iv, String url, byte errorType,
                                       int corner, int width, int height) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            getBitmapRequest(context, url)
                    .transform(new CenterCrop(), new GlideRoundTransform(corner))
                    //获得第1帧图片 这里的第一个参数 以微秒为单位
                    .frame(100000)
                    .override(width, height)
                    .error(createRoundErrorBitmapRequestBuilder(context, errorType, corner))
                    .into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadRoundBitmap(Context context, ImageView iv, String url, byte errorType,
                                       int corner, int width, int height, RequestListener<Bitmap> listener) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            getBitmapRequest(context, url)
                    .transform(new CenterCrop(), new GlideRoundTransform(corner))
                    //获得第1帧图片 这里的第一个参数 以微秒为单位
                    .frame(100000)
                    .override(width, height)
                    .listener(listener)
                    .error(createRoundErrorBitmapRequestBuilder(context, errorType, corner))
                    .into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadRoundBitmap(Context context, ImageView iv, String url, byte errorType, int corner,
                                       boolean isLeftTop, boolean isRightTop, boolean isLeftBottom, boolean isRightBottom) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            getBitmapRequest(context, url)
                    .transform(new CenterCrop(), new GlideRoundTransform(corner)
                            .setLeftTop(isLeftTop).setLeftBottom(isLeftBottom).setRightTop(isRightTop).setRightBottom(isRightBottom))
                    .error(createRoundErrorRequestBuilder(context, true, errorType, corner, isLeftTop, isRightTop, isLeftBottom, isRightBottom))
                    .into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadRoundBitmap(Context context, ImageView iv, String url, byte errorType, int corner, int width, int height,
                                       boolean isLeftTop, boolean isRightTop, boolean isLeftBottom, boolean isRightBottom) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            getBitmapRequest(context, url)
                    .transform(new CenterCrop(), new GlideRoundTransform(corner)
                            .setLeftTop(isLeftTop).setLeftBottom(isLeftBottom).setRightTop(isRightTop).setRightBottom(isRightBottom))
                    .override(width, height)
                    .error(createRoundErrorRequestBuilder(context, true, errorType, corner, isLeftTop, isRightTop, isLeftBottom, isRightBottom))
                    .into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadRoundBitmapWithStroke(Context context, ImageView iv, int resId, int corner, int strokeWidth, int strokeColor) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            RequestBuilder<Bitmap> transform = Glide.with(context)
                    .asBitmap()
                    .load(resId)
                    .transform(new CenterCrop(), new GlideRoundTransform(corner, strokeWidth, strokeColor));
            transform.into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadRoundBitmapWithStroke(Context context, ImageView iv, String url, byte errorType, int corner,
                                                 int strokeWidth, int strokeColor) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            getBitmapRequest(context, url)
                    .transform(new CenterCrop(), new GlideRoundTransform(corner, strokeWidth, strokeColor))
                    .error(createRoundErrorRequestBuilderAsBitmap(context, errorType, corner))
                    .into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadRoundBitmapWithStroke(Context context, ImageView iv, String url, byte errorType, int corner,
                                                 int width, int height, int strokeWidth, int strokeColor) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            RequestBuilder<Bitmap> requestBuilder = getBitmapRequest(context, url)
                    .transform(new CenterCrop(), new GlideRoundTransform(corner, strokeWidth, strokeColor))
                    .error(createRoundErrorRequestBuilderAsBitmap(context, errorType, corner));
            if (width > 0 && height > 0)
                requestBuilder = requestBuilder.override(width, height);
            requestBuilder.into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadRoundBitmapFitCenter(Context context, ImageView iv, String url, byte errorType, int corner,
                                                RequestListener<Bitmap> listener) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            getBitmapRequest(context, url)
                    .transform(new FitCenter(), new GlideRoundTransform(corner).setHideRadiusWhenNoMatch(true))
                    .listener(listener)
                    .error(createRoundErrorRequestBuilderAsBitmap(context, errorType, corner))
                    .into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // endregion

    public static void loadRoundVideoImage(Context context, final ImageView iv, String url, final byte errorType, final int corner) {
        if (checkParamsIllegal(context, iv)) return;
        Drawable drawable = iv.getDrawable();
        MediaUtils.getImageForVideo(context, url, (context1, file) -> {
            if (drawable == iv.getDrawable())
                try {
                    if (file != null)
                        loadRoundBitmap(context1, iv, file.getAbsolutePath(), errorType, corner);
                    else
                        loadRoundDefaultPicByErrorType(context1, iv, corner, errorType);
                } catch (Exception ignored) {
                    loadRoundDefaultPicByErrorType(context1, iv, corner, errorType);
                }
        });
    }

    public static void loadRoundVideoImage(Context context, final ImageView iv, String url, final byte errorType,
                                           final int corner, int width, int height) {
        if (checkParamsIllegal(context, iv)) return;
        Drawable drawable = iv.getDrawable();
        MediaUtils.getImageForVideo(context, url, (context1, file) -> {
            if (drawable == iv.getDrawable())
                try {
                    if (file != null)
                        loadRoundBitmap(context1, iv, file.getAbsolutePath(), errorType, corner, width, height);
                    else
                        loadRoundDefaultPicByErrorType(context1, iv, corner, errorType);
                } catch (Exception ignored) {
                    loadRoundDefaultPicByErrorType(context1, iv, corner, errorType);
                }
        });
    }

    public static void loadRoundVideoImageFitCenter(Context context, final ImageView iv, String url, final byte errorType, final int corner) {
        if (checkParamsIllegal(context, iv)) return;
        Drawable drawable = iv.getDrawable();
        if (drawable == iv.getDrawable())
            MediaUtils.getImageForVideo(context, url, (context1, file) -> {
                try {
                    if (file != null)
                        loadRoundFitCenter(context1, iv, file.getAbsolutePath(), errorType, corner);
                    else
                        loadRoundDefaultPicByErrorType(context1, iv, corner, errorType);
                } catch (Exception ignored) {
                    loadRoundDefaultPicByErrorType(context1, iv, corner, errorType);
                }
            });
    }

    public static void loadRoundVideoImageWithStroke(Context context, final ImageView iv, String url, final byte errorType, final int corner,
                                                     final int width, final int height, final int strokeWidth, final int strokeColor) {
        if (checkParamsIllegal(context, iv)) return;
        Drawable drawable = iv.getDrawable();
        MediaUtils.getImageForVideo(context, url, (context1, file) -> {
            if (drawable == iv.getDrawable())
                try {
                    if (file != null)
                        loadRoundBitmapWithStroke(context1, iv, file.getAbsolutePath(), errorType, corner, width, height, strokeWidth, strokeColor);
                    else
                        loadRoundDefaultPicByErrorType(context1, iv, corner, errorType);
                } catch (Exception ignored) {
                    loadRoundDefaultPicByErrorType(context1, iv, corner, errorType);
                }
        });
    }

    public static void loadRound(Context context, ImageView iv, int resId, int corner) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            Glide.with(context)
                    .load(resId)
                    .transform(new CenterCrop(), new GlideRoundTransform(corner))
                    .into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadRound(Context context, ImageView iv, String url, int corner) {
        if (checkParamsIllegal(context, iv)) return;
        getDrawableRequest(context, url)
                .transform(new CenterCrop(), new GlideRoundTransform(corner))
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, @Nullable Object model, @NonNull Target<Drawable> target, boolean isFirstResource) {
                        iv.setVisibility(ImageView.INVISIBLE);
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(@NonNull Drawable resource, @NonNull Object model, Target<Drawable> target, @NonNull DataSource dataSource, boolean isFirstResource) {
                        iv.setVisibility(ImageView.VISIBLE);
                        return false;
                    }
                })
                .into(iv);
    }

    public static void loadRound(Context context, ImageView iv, String url, int corner, int errorId) {
        if (checkParamsIllegal(context, iv)) return;
        getDrawableRequest(context, url)
                .transform(new CenterCrop(), new GlideRoundTransform(corner))
                .error(errorId)
                .into(iv);
    }

    public static void loadRound(Context context, ImageView iv, String url, byte errorType, int corner) {
        if (checkParamsIllegal(context, iv)) return;
        getDrawableRequest(context, url)
                .transform(new CenterCrop(), new GlideRoundTransform(corner))
                .error(createRoundErrorRequestBuilder(context, errorType, corner))
                .into(iv);
    }

    public static void loadRound(Context context, ImageView iv, String url, byte errorType, int corner,
                                 boolean isLeftTop, boolean isRightTop, boolean isLeftBottom, boolean isRightBottom) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            getDrawableRequest(context, url)
                    .transform(new CenterCrop(), new GlideRoundTransform(corner)
                            .setCircleCorner(isLeftTop, isRightTop, isLeftBottom, isRightBottom))
                    .error(createRoundErrorRequestBuilder(context, errorType, corner, isLeftTop, isRightTop, isLeftBottom, isRightBottom))
                    .into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadRound(Context context, ImageView iv, String url, byte errorType, int corner, int width, int height) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            RequestBuilder<Drawable> transform = getDrawableRequest(context, url)
                    .transform(new CenterCrop(), new GlideRoundTransform(corner))
                    .error(createRoundErrorRequestBuilder(context, errorType, corner));
            if (width > 0 && height > 0)
                transform = transform.override(width, height);
            transform.into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadRoundFitCenter(Context context, ImageView iv, String url, byte errorType, int corner) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            getDrawableRequest(context, url)
                    .transform(new FitCenter(), new GlideRoundTransform(corner).setHideRadiusWhenNoMatch(true))
                    .error(createRoundErrorRequestBuilderFitCenter(context, errorType, corner))
                    .into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadRoundWithStroke(Context context, ImageView iv, int resId, int corner, int strokeWidth, int strokeColor) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            Glide.with(context)
                    .load(resId)
                    .transform(new CenterCrop(), new GlideRoundTransform(corner, strokeWidth, strokeColor))
                    .into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadRoundWithStroke(Context context, ImageView iv, String url, byte errorType, int corner,
                                           int strokeWidth, int strokeColor) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            getDrawableRequest(context, url)
                    .transform(new CenterCrop(), new GlideRoundTransform(corner, strokeWidth, strokeColor))
                    .error(createRoundErrorRequestBuilder(context, errorType, corner))
                    .into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadRoundWithStroke(Context context, ImageView iv, String url, byte errorType, int corner,
                                           int width, int height, int strokeWidth, int strokeColor) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            RequestBuilder<Drawable> error = getDrawableRequest(context, url)
                    .transform(new CenterCrop(), new GlideRoundTransform(corner, strokeWidth, strokeColor))
                    .error(createRoundErrorRequestBuilder(context, errorType, corner));
            if (width > 0 && height > 0)
                error = error.override(width, height);
            error.into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static RequestBuilder createRoundErrorRequestBuilder(Context context, byte errorType, int corner) {
        return createRoundErrorRequestBuilder(context, errorType, corner, true, true, true, true);
    }

    private static RequestBuilder createRoundErrorRequestBuilder(Context context, byte errorType, int corner,
                                                                 boolean isLeftTop, boolean isRightTop, boolean isLeftBottom, boolean isRightBottom) {
        return createRoundErrorRequestBuilder(context, false, errorType, corner, isLeftTop, isRightTop, isLeftBottom, isRightBottom);
    }

    private static RequestBuilder createRoundErrorBitmapRequestBuilder(Context context, byte errorType, int corner) {
        return createRoundErrorBitmapRequestBuilder(context, errorType, corner, true, true, true, true);
    }

    private static RequestBuilder createRoundErrorBitmapRequestBuilder(Context context, byte errorType, int corner,
                                                                       boolean isLeftTop, boolean isRightTop, boolean isLeftBottom, boolean isRightBottom) {
        return createRoundErrorRequestBuilder(context, true, errorType, corner, isLeftTop, isRightTop, isLeftBottom, isRightBottom);
    }

    private static RequestBuilder createRoundErrorRequestBuilder(Context context, boolean isAsBitmap, byte errorType, int corner,
                                                                 boolean isLeftTop, boolean isRightTop, boolean isLeftBottom, boolean isRightBottom) {
        RequestManager glide = Glide.with(context);
        RequestBuilder<?> builder;
        if (isAsBitmap)
            builder = glide.asBitmap();
        else
            builder = glide.asDrawable();
        return builder.load(getErrorByType(errorType))
                .transform(new CenterCrop(), new GlideRoundTransform(corner)
                        .setCircleCorner(isLeftTop, isRightTop, isLeftBottom, isRightBottom));
    }

    private static RequestBuilder createRoundErrorRequestBuilder(Context context, int corner, int errorId) {
        RequestManager glide = Glide.with(context);
        return glide.load(errorId)
                .transform(new CenterCrop(), new GlideRoundTransform(corner));
    }

    private static RequestBuilder createRoundErrorRequestBuilderWithStroke(Context context, byte errorType, int corner,
                                                                           int strokeWidth, int strokeColor) {
        return createRoundErrorRequestBuilderAsBitmapWithStroke(context, false, errorType, corner,
                true, true, true, true, strokeWidth, strokeColor);
    }

    private static RequestBuilder createRoundErrorRequestBuilderFitCenter(Context context, byte errorType, int corner) {
        return Glide.with(context).load(getErrorByType(errorType)).transform(new FitCenter(), new GlideRoundTransform(corner).setHideRadiusWhenNoMatch(true));
    }

    private static RequestBuilder createRoundErrorRequestBuilderAsBitmap(Context context, byte errorType, int corner) {
        return createRoundErrorRequestBuilder(context, true, errorType, corner, true, true, true, true);
    }

    private static RequestBuilder createRoundErrorRequestBuilderFitCenterAsBitmap(Context context, byte errorType, int corner) {
        return Glide.with(context).asBitmap().load(getErrorByType(errorType)).transform(new FitCenter(), new GlideRoundTransform(corner).setHideRadiusWhenNoMatch(true));
    }

    private static RequestBuilder createRoundErrorRequestBuilderAsBitmapWithStroke(Context context, boolean isAsBitmap, byte errorType, int corner,
                                                                                   boolean isLeftTop, boolean isRightTop, boolean isLeftBottom, boolean isRightBottom,
                                                                                   int strokeWidth, int strokeColor) {
        RequestManager glide = Glide.with(context);
        RequestBuilder<?> builder;
        if (isAsBitmap)
            builder = glide.asBitmap();
        else
            builder = glide.asDrawable();
        return builder.load(getErrorByType(errorType)).transform(new CenterCrop(),
                new GlideRoundTransform(corner, strokeWidth, strokeColor)
                        .setCircleCorner(isLeftTop, isRightTop, isLeftBottom, isRightBottom));
    }
    // endregion

    // region preload
    public static void preload(Context context, String url) {
        try {
            getDrawableRequest(context, url)
                    .preload();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void preloadDrawable(Context context, String url, RequestListener<Drawable> listener) {
        try {
            getDrawableRequest(context, url)
                    .listener(listener)
                    .preload();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // endregion

    // region 加载圆角的默认图片
    public static final byte TYPE_NORMAL = 0;
    public static final byte TYPE_NORMAL_DEEP = 1;
    public static final byte TYPE_SMALL = 2;
    public static final byte TYPE_SMALL_DEEP = 3;
    public static final byte TYPE_LONG = 4;
    public static final byte TYPE_LONG_DEEP = 5;
    public static final byte TYPE_HEAD = 6;

    public static void loadRoundDefaultPic(Context context, ImageView imageView, int corner) {
        loadRoundDefaultPicByErrorType(context, imageView, corner, TYPE_NORMAL);
    }

    public static void loadRoundDefaultPicDeep(Context context, ImageView imageView, int corner) {
        loadRoundDefaultPicByErrorType(context, imageView, corner, TYPE_NORMAL_DEEP);
    }

    public static int getErrorByType(byte errorType) {
        switch (errorType) {
            case TYPE_HEAD:
                return R.mipmap.head_default;
            case TYPE_SMALL:
            case TYPE_SMALL_DEEP:
            case TYPE_LONG:
            case TYPE_LONG_DEEP:
            case TYPE_NORMAL_DEEP:
            case TYPE_NORMAL:
            default:
                return R.mipmap.default_pic;
        }
    }

    public static void loadRoundDefaultPicByErrorType(Context context, ImageView iv, int corner, byte errorType) {
        loadRoundDefaultPicByErrorType(context, iv, corner, errorType, true, true, true, true);
    }

    public static void loadRoundDefaultPicByErrorType(Context context, ImageView iv, int corner, byte errorType,
                                                      boolean isLeftTop, boolean isRightTop, boolean isLeftBottom, boolean isRightBottom) {
        if (checkParamsIllegal(context, iv)) return;
        try {
            int errorResId = getErrorByType(errorType);
            RequestBuilder<Drawable> request = Glide.with(context)
                    .load(errorResId);
            if (corner > 0) {
                request = request.transform(new CenterCrop(),
                        new GlideRoundTransform(corner).setLeftTop(isLeftTop).setRightTop(isRightTop).setLeftBottom(isLeftBottom).setRightBottom(isRightBottom));
            } else {
                request = request.centerCrop();
            }
            request.into(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // endregion
}
